<?php 
	defined("AT_EXE") or die ("CORE VARIABLE IS NOT DEFINED.");
	class Controller extends AT {
		protected $model = null ;
		protected $datas = array();
		private $view = NULL ;
		protected  $layout = "" ;
		private $route = array();
		private $services = array();
		private $calledClass= "" ;
		private $viewName= "";
		public function __construct(){
			parent::__construct();
		}
		public function getRoute(){
			return $this->route;
		}
		public function set($key,$val=""){
			if(is_array($key)&&!empty($key))$this->datas = array_merge($this->datas,$key);
			else if(isset($key)&&isset($val)&&$key!="")$this->datas= array_merge($this->datas,array($key=>$val));
		}
		public function setDatas($datas=array()){
			$this->datas = $datas ;
		}
		public function getDatas(){
			return $this->datas;
		}
		public function get($key){
			return isset($this->datas[$key])?$this->datas[$key]:NULL;
		}
		public function calledClass($calledClass=""){
			if($calledClass!=""){
				$this->calledClass = $calledClass;
				$this->viewName=strtolower(Basic::getPlural(str_replace("Controller","",$this->calledClass)));
				$this->model=Basic::fileExist(APP_MODEL.str_replace("Controller","",$this->calledClass).".php")?$this->loadModel(str_replace("Controller","",$this->calledClass)):null;
			}
		} 
		public function render($layout=""){
			if(isset($this->layout)&&$this->layout!==""&&$this->layout!==null)$layout = array("layouts"=>$this->layout);
			else if(is_array($layout)&&!empty($layout))foreach($layout AS $controller => $layout )$layout = array(strtolower(Basic::getPlural($controller))=>$layout);
			else $layout = array($this->viewName=>$layout);
			$this->view = new View($layout,$this->route["controller"],$this->datas);
			$this->view->render();
		}
		public function setRoute($route){
			$this->route=$route;
			$this->datas = $route["request"]->getVars();
		}
		public function setLevel($level){
			$actions = get_class_methods( get_called_class() ) ;
			$this->services[$level][] = array("controller"=>get_called_class(),"action"=>$actions[0]);
		}
		public function getLevel(){
			return $this->services;
		}
		public function redirect($path=array("page"=>"index"),$vars=array()){
			header("location:".$this->getPath($path,$vars));
		}
		public function refresh($path=array("page"=>"index"),$vars=array(),$second=3){
			header("refresh:".$second.",url=".$this->getPath($path,$vars));
		}
		public function getPath($path=array("page"=>"index"),$vars=array()){
			if(!empty($path)){
				if(isset($_SERVER["QUERY_STRING"])&&$_SERVER["QUERY_STRING"]!==""){
					$link = array();
					foreach($path as $controller =>$action ){
						$link[]="com=".$controller."&action=".$action;
						foreach($vars as $key =>$value )$link[]=$key."=".$value;
						$link = "index.php?".implode("&",$link) ;
						break;
					}
				}
				else{
					foreach($path as $controller =>$action ){
						$link = "index.php/".$controller."/".$action;
						foreach($vars as $key=>$val)$link.="/".$key.":".$val;
						break;
					}
				}
				return ROOT . $link;
			}
		}
	}
